package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.ulits.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 套餐控制层
 *
 * @author ：zengjie
 * @date ：2020/11/7
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 新增图片
     * @param imgFile
     * @return
     */
    @PreAuthorize("hasAnyAuthority('SETMEAL_ADD')")
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Result upload(MultipartFile imgFile){
        try {
            //1.获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //2.将原始文件名称 生成一个唯一文件名称(自定义文件名)
            //获取原始文件名后缀
            String suffix = originalFilename.substring(originalFilename.indexOf("."));
            //生成唯一文件名
            String newFileName = UUID.randomUUID().toString()+suffix;
            //3.调用七牛云工具类文件上传
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),newFileName);
            //当七牛云上传图片成功后，将记录写入redis
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,newFileName);
            //4.将文件名称返回给页面
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,newFileName);//文件名称一定要返回页面
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PreAuthorize("hasAnyAuthority('SETMEAL_ADD')")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
       try {
           setmealService.add(setmeal,checkgroupIds);
           jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
           return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
       }
   }

    /**
     * 套餐分页
     */
    @RequestMapping(value = "/findPage",method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            return setmealService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据套餐id查询套餐数据
     */
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(Integer setmealId){
        try {
            Setmeal setmeal = setmealService.findById(setmealId);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 根据套餐id查询检查组ids
     */
    @RequestMapping(value = "/findGroupIdsBySetmealId",method = RequestMethod.GET)
    public List<Integer> findGroupIdsBySetmealId(Integer setmealId){
        try {
            return setmealService.findGroupIdsBySetmealId (setmealId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 编辑套餐
     */
    @PreAuthorize("hasAnyAuthority('SETMEAL_EDIT')")
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Result edit(@RequestBody Setmeal setmeal,Integer [] checkgroupIds){
        try {
            setmealService.edit(setmeal,checkgroupIds);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    /**
     * 删除套餐
     */
    @PreAuthorize("hasAnyAuthority('SETMEAL_DELETE')")
    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    public Result deleteById(Integer setmealId){
        try {
            setmealService.deleteById(setmealId);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

    /**
     * 初始化套餐列表静态页面和所有套餐详情页面
     */
    @RequestMapping("/a")
    public void a(){
        setmealService.test();
    }
}
